<?php
/**
 * @since      File available since Release v1.1
 */
defined('Inbearshop') or exit('Access Invalid!');
define('MYSQL_RESULT_TYPE',1);
class crontabControl extends Control{
	public function __construct(){
		parent::__construct();
	}
	
	public function indexOp(){
		$this->yikoujiaorderOp();
		$this->createorderOp();
		$this->lingxianOp();
		$this->exchangefailOp();
		$this->confirmshouhuoOp();
		$this->confirmcommentOp();
		$this->meifahuoOp();
		$this->daoqitixingOp();
		$this->clearyaoOp();
		$this->usermeifahuoOp();
		$this->meidealtuikuanOp();
		$this->meidealtuiOp();
	}
	
	/*
	 * 小熊Shop
	 * 一口价-定时服务
	 */
	public function yikoujiaorderOp(){
		$condition = array();
		$condition['auction.yikoujia_end'] =1;
		$condition['auction.end_time'] = array('gt',time());
		$condition['auction.auction_order_sn'] = '0';
		
		$model = Model();
		$field = 'auction.id as id,auction.cashprice as cashprice,auction.is_state as is_state,auction.auction_description as auction_description,auction.auction_order_sn as auction_order_sn,auction.seller_id as seller_id,auction.seller_name as seller_name,auction.leadingprice as leadingprice,';
		$field.='auction_log.user_id as user_id,auction_log.nickname as nickname,auction.auction_log_id,auction.end_time as end_time';
		$on = 'auction.auction_log_id = auction_log.id';
		$list = $model->field($field)->table('auction,auction_log')->join('left')->on($on)->where($condition)->select();
		
		if(!empty($list)){
			foreach($list as $val){
				
				$params = array();
				$params['order_sn'] = $this->snOrder();
				$params['order_auction_id'] = $val['id'];
				$params['seller_id'] = $val['seller_id'];
				$params['seller_name'] = $val['seller_name'];
				$params['user_id'] = $val['user_id'];
				$params['user_name'] = $val['nickname'];
				$params['order_state'] = 0;
				$params['add_time'] = time();
				$params['price'] = $val['leadingprice'];
				
				$model = Model();
				$res = $model->table('order')->insert($params,true);
				if($res){
						
					$user = $model->table('user')->where(array('user_id'=>$val['user_id']))->find();
						
					$data = array();
					$data['touser'] = $user['openid'];
					$data['template_id'] = $this->getTemplateId(3);
					$data['url'] = BASE_SITE_URL.'/index.php?act=order&op=orderuserdetail&order_id='.$res;
					$data['data']['first']['value'] = '恭喜你购买成功！';
					$data['data']['keyword1']['value'] = $val['auction_description'];
					$data['data']['keyword2']['value'] = $val['leadingprice'].'元';
					$data['data']['remark']['value'] = '请在48小时之内付款（点击此处直接支付）';
					$data1 = json_encode($data);
					$this->send_template_message($data1);
						
					$seller = $model->table('user')->where(array('user_id'=>$val['seller_id']))->find();
					$data = array();
					$data['touser'] = $seller['openid'];
					$data['template_id'] = $this->getTemplateId(3);
					$data['url'] = BASE_SITE_URL.'/index.php?act=order&op=ordersellerdetail&auction_id='.$val['id'];
					$data['data']['first']['value'] = "尊敬的【{$seller['nickname']}】,您的拍品已被{$user['nickname']}拍得";
					$data['data']['keyword1']['value'] = $val['auction_description'];
					$data['data']['keyword2']['value'] = $val['leadingprice'].'元';
					$data1 = json_encode($data);
					$this->send_template_message($data1);
						
					$message = "恭喜您<br>您的拍品已经被{$val['nickname']}拍得";
					$this->addsellerlog($val['id'],$message,$val['seller_id']);
				
					$message = "恭喜您,竞拍成功！尊敬的{$val['nickname']}，恭喜您竞拍成功";
					$this->addsellerlog($val['id'],$message,$val['user_id']);
						
					$model->table('auction')->where(array('id'=>$val['id']))->update(array('auction_order_sn'=>$params['order_sn']));
					
					if($val['cashprice']>0){
						$condition = array();
						$condition['cash_log.auction_id'] = $val['id'];
						$condition['cash_log.is_cash'] = 1;
						
						$field = 'cash_log.*,user.openid as openid,user.nickname as nickname';
						$cash_log = $model->field($field)->table('cash_log,user')->join('left')->on('cash_log.user_id = user.user_id')->where($condition)->select();
						if(!empty($cash_log)){
							foreach($cash_log as $xxx){
								if($xxx['user_id'] == $val['user_id'] || $xxx['user_id'] == $val['seller_id']){
									continue;
								}
						
								$params = array();
								$params['user_id'] = $xxx['user_id'];
								$params['mingxi_type'] = '0';
								$params['mingxi_price'] = $xxx['cprice'];
								$params['add_time'] = time();
								$params['mingxi_reason'] = '保证金退还';
								$params['mingxi_pos'] = '0';
								$model->table('mingxi')->insert($params);
						
								if($xxx['paymentname']=='weixin'){
									require_once BASE_ROOT_PATH."/shop/templates/default/home/WxPayPubHelper/WxPayPubHelper.php";
									$out_trade_no = $xxx['pre_order_sn'];
									$refund_fee = $xxx["cprice"]*100;
									//商户退款单号，商户自定义，此处仅作举例
									$out_refund_no = "refund_cash_sn".time();
									//总金额需与订单号out_trade_no对应，demo中的所有订单的总金额为1分
									$total_fee = $xxx["cprice"]*100;
									//使用退款接口
									$refund = new Refund_pub();
									$refund->setParameter("out_trade_no","$out_trade_no");//商户订单号
									$refund->setParameter("out_refund_no","$out_refund_no");//商户退款单号
									$refund->setParameter("total_fee","$total_fee");//总金额
									$refund->setParameter("refund_fee","$refund_fee");//退款金额
									$refund->setParameter("op_user_id",WxPayConf_pub::MCHID);//操作员
									//调用结果
									$refundResult = $refund->getResult();
									
								}else{
									$model->table('user')->where(array('user_id'=>$xxx['user_id']))->setInc('price',$xxx['cprice']);
								}
									
								$data = array();
								$data1 = array();
								$data['touser'] = $xxx['openid'];
								$data['template_id'] = $this->getTemplateId(6);
								$data['url'] = BASE_SITE_URL.'/index.php?act=index&auction_id='.$val['id'];
								$data['data']['first']['value'] = "尊敬的【{$xxx['nickname']}】,您发布的‘{$val['auction_description']}’拍品未成交，保证金退回至余额";
								$data['data']['reason']['value'] = '保证金退还';
								$data['data']['refund']['value'] = $val['cashprice'].'元';
								$data['data']['remark']['value'] = '如未收到退款，请联系客服';
									
								$data1 = json_encode($data);
								$this->send_template_message($data1);
							}
						}
					}

				}
			}
		}	
		
		echo 'yikoujia';
	}
	
	/*
	 * 小熊Shop
	 * 创建订单-定时服务
	 */
	public function createorderOp(){

		$condition = array();
 		$condition['auction.end_time'] = array('lt',time());
		$condition['auction.is_liupai'] = '1';
		$condition['auction.is_fabu'] = '1';
		$condition['auction.is_state'] = '0';
		$condition['auction.auction_order_sn'] = '0';
		
		$model = Model();
		$field = 'auction.id as id,auction.cashprice as cashprice,auction.is_state as is_state,auction.auction_description as auction_description,auction.auction_order_sn as auction_order_sn,auction.seller_id as seller_id,auction.seller_name as seller_name,auction.leadingprice as leadingprice,';	
		$field.='auction_log.user_id as user_id,auction_log.nickname as nickname,auction.auction_log_id,auction.end_time as end_time';
		$on = 'auction.auction_log_id = auction_log.id';
		$list = $model->field($field)->table('auction,auction_log')->join('left')->on($on)->where($condition)->order('id desc')->select();
		//所有过期拍品

		if(!empty($list)){			
			foreach($list as $val){		
				$order = $model->table('order')->where(array('order_auction_id'=>$val['id']))->find();
				if(!empty($order)) continue;
			
				if($val['auction_log_id']=='0'){
					$model->table('auction')->where(array('id'=>$val['id']))->update(array('is_liupai'=>2));
					$message = "Sorry!拍卖失败<br>尊敬的卖家，您的拍品:{$val['auction_description']},再截至时间无人出价";
					$this->addsellerlog($val['id'],$message,$val['seller_id']);
	
					$seller = $model->table('user')->where(array('user_id'=>$val['seller_id']))->find();
					if($val['cashprice']>0){//流拍保证金退还
						
						$cash_log = $model->table('cash_log')->where(array('auction_id'=>$val['id']))->find();
						if($cash_log['paymentname']=='weixin'){
							require_once BASE_ROOT_PATH."/shop/templates/default/home/WxPayPubHelper/WxPayPubHelper.php";
							$out_trade_no = $cash_log['pre_order_sn'];
							$refund_fee = $cash_log["cprice"]*100;
							//商户退款单号，商户自定义，此处仅作举例
							$out_refund_no = "refund_cash_sn".time();
							//总金额需与订单号out_trade_no对应，demo中的所有订单的总金额为1分
							$total_fee = $cash_log["cprice"]*100;
							//使用退款接口
							$refund = new Refund_pub();
							$refund->setParameter("out_trade_no","$out_trade_no");//商户订单号
							$refund->setParameter("out_refund_no","$out_refund_no");//商户退款单号
							$refund->setParameter("total_fee","$total_fee");//总金额
							$refund->setParameter("refund_fee","$refund_fee");//退款金额
							$refund->setParameter("op_user_id",WxPayConf_pub::MCHID);//操作员
							//调用结果
							$refundResult = $refund->getResult();
						}else{
							$model->table('user')->where(array('user_id'=>$val['seller_id']))->setInc('price',$val['cashprice']);
						}						

						
						$params = array();
						$params['user_id'] = $val['seller_id'];
						$params['mingxi_type'] = '0';
						$params['mingxi_price'] = $val['cashprice'];
						$params['add_time'] = time();
						$params['mingxi_reason'] = '保证金退还';
						$params['mingxi_pos'] = '0';
						$model->table('mingxi')->insert($params);
						
						$data = array();
						$data1 = array();
						$data['touser'] = $seller['openid'];
						$data['template_id'] = $this->getTemplateId(6);
						$data['url'] = BASE_SITE_URL.'/index.php?act=order&op=ordersellerdetail&auction_id='.$val['id'];
						$data['data']['first']['value'] = "尊敬的【{$val['seller_name']}】,您发布的‘{$val['auction_description']}’拍品未成交，保证金退回至余额";
						$data['data']['reason']['value'] = '流拍';
						$data['data']['refund']['value'] = $val['cashprice'].'元';
						$data['data']['remark']['value'] = '如未收到退款，请联系客服';
						
						$data1 = json_encode($data);
						$this->send_template_message($data1);
					}
					
					
					$data = array();
					$data1 = array();
					$data['touser'] = $seller['openid'];
					$data['template_id'] = $this->getTemplateId(12);
					$data['url'] = BASE_SITE_URL.'/index.php?act=order&op=ordersellerdetail&auction_id='.$val['id'];
					$data['data']['first']['value'] = "尊敬的【{$val['seller_name']}】,您的拍品‘{$val['auction_description']}’在截至时间内无人出价";
					$data['data']['tradeDateTime']['value'] = date("Y-m-d H:i",$val['end_time']);					
					$data['data']['tradeType']['value'] = '流拍';
					$data['data']['curAmount']['value'] = '无人出价';
					
					$data1 = json_encode($data);
					$this->send_template_message($data1);
					
					continue;
				}
				
				$params = array();
				$params['order_sn'] = $this->snOrder();
				$params['order_auction_id'] = $val['id'];
				$params['seller_id'] = $val['seller_id'];
				$params['seller_name'] = $val['seller_name'];
				$params['user_id'] = $val['user_id'];
				$params['user_name'] = $val['nickname'];
				$params['order_state'] = 0;
				$params['add_time'] = time();
				$params['price'] = $val['leadingprice'];

				$model = Model();
				$res = $model->table('order')->insert($params,true);
				if($res){
					
					$model->table('auction')->where(array('id'=>$val['id']))->update(array('auction_order_sn'=>$params['order_sn']));
						
					$user = $model->table('user')->where(array('user_id'=>$val['user_id']))->find();
					
					$data = array();
					$data['touser'] = $user['openid'];
					$data['template_id'] = $this->getTemplateId(3);
					$data['url'] = BASE_SITE_URL.'/index.php?act=order&op=orderuserdetail&order_id='.$res;
					$data['data']['first']['value'] = '恭喜你购买成功！';
					$data['data']['keyword1']['value'] = $val['auction_description'];
					$data['data']['keyword2']['value'] = $val['leadingprice'].'元';
					$data['data']['remark']['value'] = '请在48小时之内付款（点击此处直接支付）';
					$data1 = json_encode($data);
					$this->send_template_message($data1);
					
					$seller = $model->table('user')->where(array('user_id'=>$val['seller_id']))->find();
					$data = array();
					$data['touser'] = $seller['openid'];
					$data['template_id'] = $this->getTemplateId(3);
					$data['url'] = BASE_SITE_URL.'/index.php?act=order&op=ordersellerdetail&auction_id='.$val['id'];
					$data['data']['first']['value'] = "尊敬的【{$seller['nickname']}】,您的拍品已被{$user['nickname']}拍得";
					$data['data']['keyword1']['value'] = $val['auction_description'];
					$data['data']['keyword2']['value'] = $val['leadingprice'].'元';
					$data1 = json_encode($data);
					$this->send_template_message($data1);
					
					$message = "恭喜您<br>您的拍品已经被{$val['nickname']}拍得";
					$this->addsellerlog($val['id'],$message,$val['seller_id']);		

					$message = "恭喜您,竞拍成功！尊敬的{$val['nickname']}，恭喜您竞拍成功";
					$this->addsellerlog($val['id'],$message,$val['user_id']);
					
					
					if($val['cashprice']>0){
						$condition = array();
						$condition['cash_log.auction_id'] = $val['id'];
						$condition['cash_log.is_cash'] = 1;
						
						$field = 'cash_log.*,user.openid as openid,user.nickname as nickname';
						$cash_log = $model->field($field)->table('cash_log,user')->join('left')->on('cash_log.user_id = user.user_id')->where($condition)->select();
						if(!empty($cash_log)){
							foreach($cash_log as $xxx){
								if($xxx['user_id'] == $val['user_id'] || $xxx['user_id'] == $val['seller_id']){
									continue;
								}
						
								$params = array();
								$params['user_id'] = $xxx['user_id'];
								$params['mingxi_type'] = '0';
								$params['mingxi_price'] = $xxx['cprice'];
								$params['add_time'] = time();
								$params['mingxi_reason'] = '保证金退还';
								$params['mingxi_pos'] = '0';
								$model->table('mingxi')->insert($params);
								
								if($xxx['paymentname']=='weixin'){
									require_once BASE_ROOT_PATH."/shop/templates/default/home/WxPayPubHelper/WxPayPubHelper.php";
									$out_trade_no = $xxx['pre_order_sn'];
									$refund_fee = $xxx["cprice"]*100;
									//商户退款单号，商户自定义，此处仅作举例
									$out_refund_no = "refund_cash_sn".time();
									//总金额需与订单号out_trade_no对应，demo中的所有订单的总金额为1分
									$total_fee = $xxx["cprice"]*100;
									//使用退款接口
									$refund = new Refund_pub();
									$refund->setParameter("out_trade_no","$out_trade_no");//商户订单号
									$refund->setParameter("out_refund_no","$out_refund_no");//商户退款单号
									$refund->setParameter("total_fee","$total_fee");//总金额
									$refund->setParameter("refund_fee","$refund_fee");//退款金额
									$refund->setParameter("op_user_id",WxPayConf_pub::MCHID);//操作员
									//调用结果
									$refundResult = $refund->getResult();
									
								}else{
									$model->table('user')->where(array('user_id'=>$xxx['user_id']))->setInc('price',$xxx['cprice']);
								}
																
									
								$data = array();
								$data1 = array();
								$data['touser'] = $xxx['openid'];
								$data['template_id'] = $this->getTemplateId(6);
								$data['url'] = BASE_SITE_URL.'/index.php?act=index&auction_id='.$val['id'];
								$data['data']['first']['value'] = "尊敬的【{$xxx['nickname']}】,您发布的‘{$val['auction_description']}’拍品未成交，保证金退回至余额";
								$data['data']['reason']['value'] = '保证金退还';
								$data['data']['refund']['value'] = $val['cashprice'].'元';
								$data['data']['remark']['value'] = '如未收到退款，请联系客服';
									
								$data1 = json_encode($data);
								$this->send_template_message($data1);
							}
						}
					}
					
				}
			}			
		}
		echo 'createorder ok';
	}
	
	
	public function lingxianOp(){
		
		$condition = array();
 		$condition['end_time'] = array('gt',time());
		$condition['is_fabu'] = '1';
		$condition['is_state'] = '0';
		$condition['is_end_remind'] = '0';
		$model = Model();
		$list = $model->table('auction')->where($condition)->select();

		if(!empty($list)){
			foreach($list as $val){
				
				if($val['yikoujia_end']==1){
					continue;
				}
				
				if(($val['end_time']-60*55)>time()){
					continue;
				}
				
				$condition = array();
				$condition['auction_log.auction_id'] = $val['id'];
				$condition['auction_log.tuisong'] = '0';
				$field = 'auction_log.id as id,user.openid as openid,user.nickname as nickname,auction_log.user_id as user_id,auction_log.auction_id as auction_id';
				$on = 'auction_log.user_id = user.user_id';
				$userlist = $model->field($field)->table('auction_log,user')->join('left')->on($on)->where($condition)->select();

				$user = $model->table('user')->where(array('user_id'=>$val['seller_id']))->find();
				
				if($val['is_end_remind']!='1'){
					$model->table('auction')->where(array('id'=>$val['id']))->update(array('is_end_remind'=>1));
					$data = array();
					$data['touser'] = $user['openid'];
					$data['template_id'] = $this->getTemplateId(4);
					$data['url'] = BASE_SITE_URL.'/index.php?act=index&auction_id='.$val['id'];
					$data['data']['first']['value'] = "【{$user['nickname']}】您好，您的拍品‘{$val['auction_description']}’将于".date("Y-m-d H:i",$val['end_time'])."结束.";
					$data['data']['number']['value'] = "{$val['auction_sn']}";
					$data['data']['name']['value'] = "{$val['auction_description']}";
					$data['data']['deadline']['value'] = date("Y-m-d H:i",$val['end_time']);
					$data['data']['remark']['value'] = '';
					$data1 = json_encode($data);
					$this->send_template_message($data1);
				}
				
				if(!empty($userlist)){
					
					$array = array();
					foreach($userlist as $vvv){
						
						$log = array();
						$log['user_id'] = $vvv['user_id'];
						$log['auction_id'] = $vvv['auction_id'];
						$log['tuisong'] = 1;
						
						$arr = array();
						$arr = $model->table('auction_log')->where($log)->find();
						if(!empty($arr)){
							$model->table('auction_log')->where(array('id'=>$vvv['id']))->update(array('tuisong'=>1));
							continue;
						}
						
						
						$data = array();
						$data['touser'] = $vvv['openid'];
						$data['template_id'] = $this->getTemplateId(4);
						$data['url'] = BASE_SITE_URL.'/index.php?act=index&auction_id='.$val['id'];
						$data['data']['first']['value'] = "【{$vvv['nickname']}】您好，您的拍品‘{$val['auction_description']}’将于".date("Y-m-d H:i",$val['end_time'])."结束.";
						$data['data']['number']['value'] = "{$val['auction_sn']}";
						$data['data']['name']['value'] = "{$val['auction_description']}";
						$data['data']['deadline']['value'] = date("Y-m-d H:i",$val['end_time']);
						$data['data']['remark']['value'] = '';
						$data1 = json_encode($data);
						$res = $this->send_template_message($data1);	

						$array[] = $vvv['openid'];						
						$model->table('auction_log')->where(array('id'=>$vvv['id']))->update(array('tuisong'=>1));
					}
				}
			}
		}
		
		echo 'lingxian ok';
	}
	
	public function exchangefailOp(){

		$condition = array();
		$condition['order.order_state'] = '0';
		$condition['order.is_fail'] = '0';
		
		$model = Model();
		$field = 'order.id as id,order.order_sn as order_sn,order.order_state as order_state,order.add_time as add_time,auction.id as auction_id,auction.cashprice as cashprice,';
		$field.= 'order.user_id as user_id,order.seller_id as seller_id';
		$on = 'order.order_auction_id = auction.id';
		$list = $model->field($field)->table('order,auction')->join('left')->on($on)->where($condition)->select();
			
		if(!empty($list)){
			foreach($list as $val){
				if(($val['add_time']+(3600*48))<time()){
					$model->table('order')->where(array('order_auction_id'=>$val['auction_id']))->update(array('is_fail'=>'1'));
					$auction = $model->table('auction')->where(array('id'=>$val['auction_id']))->find();
					if($val['cashprice']>0){
						$seller = $model->table('user')->where(array('user_id'=>$val['seller_id']))->find();
						$price = $val['cashprice']+$seller['price'];
						$model->table('user')->where(array('user_id'=>$val['seller_id']))->update(array('price'=>$price));	

						$params = array();
						$params['user_id'] = $val['seller_id'];
						$params['mingxi_type'] = '0';
						$params['mingxi_price'] = $val['cashprice'];
						$params['add_time'] = time();
						$params['mingxi_reason'] = '买家未支付，保证金退赔偿';
						$params['mingxi_pos'] = '0';
						$model->table('mingxi')->insert($params);	

						//卖家保证金退还
						$seller_cash_log = $model->table('cash_log')->where(array('user_id'=>$val['seller_id'],'auction_id'=>$val['auction_id']))->find();
						if($seller_cash_log['paymentname']=='weixin'){
							require_once BASE_ROOT_PATH."/shop/templates/default/home/WxPayPubHelper/WxPayPubHelper.php";
							$out_trade_no = $seller_cash_log['pre_order_sn'];
							$refund_fee = $seller_cash_log["cprice"]*100;
							//商户退款单号，商户自定义，此处仅作举例
							$out_refund_no = "refund_scash_sn".time();
							//总金额需与订单号out_trade_no对应，demo中的所有订单的总金额为1分
							$total_fee = $seller_cash_log["cprice"]*100;
							//使用退款接口
							$refund = new Refund_pub();
							$refund->setParameter("out_trade_no","$out_trade_no");//商户订单号
							$refund->setParameter("out_refund_no","$out_refund_no");//商户退款单号
							$refund->setParameter("total_fee","$total_fee");//总金额
							$refund->setParameter("refund_fee","$refund_fee");//退款金额
							$refund->setParameter("op_user_id",WxPayConf_pub::MCHID);//操作员
							//调用结果
							$refundResult = $refund->getResult();
								
							$params = array();
							$params['user_id'] = $val['seller_id'];
							$params['mingxi_type'] = '0';
							$params['mingxi_price'] = $val['cashprice'];
							$params['add_time'] = time();
							$params['mingxi_reason'] = '保证金退还';
							$params['mingxi_pos'] = '1';
							$model->table('mingxi')->insert($params);
								
							$data = array();
							$data1 = array();
							$data['touser'] = $seller['openid'];
							$data['template_id'] = $this->getTemplateId(6);
							$data['url'] = BASE_SITE_URL.'/index.php?act=order&op=ordersellerdetail&auction_id='.$val['auction_id'];
							$data['data']['first']['value'] = "尊敬的【{$seller['nickname']}】,您发布的‘{$auction['auction_description']}’拍品，保证金退还至您微信钱包";
							$data['data']['reason']['value'] = '付款成功';
							$data['data']['refund']['value'] = $auction['cashprice'].'元';
							$data['data']['remark']['value'] = '如未收到退款，请联系客服';
						}else{
							$model->table('user')->where(array('user_id'=>$val['seller_id']))->setInc('price',$val['cashprice']);
			
							$data = array();
							$data1 = array();
							$data['touser'] = $seller['openid'];
							$data['template_id'] = $this->getTemplateId(6);
							$data['url'] = BASE_SITE_URL.'/index.php?act=order&op=ordersellerdetail&auction_id='.$val['auction_id'];
							$data['data']['first']['value'] = "尊敬的【{$seller['nickname']}】,您发布的‘{$auction['auction_description']}’拍品，保证金退还至您的余额";
							$data['data']['reason']['value'] = '付款成功';
							$data['data']['refund']['value'] = $auction['cashprice'].'元';
							$data['data']['remark']['value'] = '如未收到退款，请联系客服';
																
							$params = array();
							$params['user_id'] = $seller['seller_id'];
							$params['mingxi_type'] = '0';
							$params['mingxi_price'] = $auction['cashprice'];
							$params['add_time'] = time();
							$params['mingxi_reason'] = '保证金退还';
							$params['mingxi_pos'] = '0';
							$model->table('mingxi')->insert($params);
						}
						$data1 = json_encode($data);
						$this->send_template_message($data1);
					}
					
					$user = $model->table('user')->where(array('user_id'=>$val['user_id']))->find();

					$data = array();
					$data['touser'] = $user['openid'];
					$data['template_id'] = $this->getTemplateId(2);
					$data['url'] = BASE_SITE_URL.'/index.php?act=order&op=orderuserdetail&order_id='.$val['id'];
					$data['data']['first']['value'] = "尊敬的【{$user['nickname']}】，因您未及时付款，交易已被关闭";
					$data['data']['keyword1']['value'] = $auction['auction_description'];
					$data['data']['keyword2']['value'] = $auction['cashprice'].'元';
					$data['data']['keyword2']['color'] = '#1874CD';
					$data['data']['keyword3']['value'] = $val['order_sn'];
					$data['data']['keyword4']['value'] = date("m月d日 H:i",($val['add_time']));
					$data['data']['keyword5']['value'] = date("m月d日 H:i",($val['add_time']+(3600*48)));
					$wx_name_arr = F('wxaccount');
					$wx_name = $wx_name_arr['wx_name'];
					$data['data']['remark']['value'] = "感谢您的支持，如有疑问请咨询{$wx_name}客服";
					
					$data1 = json_encode($data);
					$res = $this->send_template_message($data1);
					if($user['upoints']>0){
						$model->table('user')->where(array('user_id'=>$val['user_id']))->setDec('upoints',1);
					}
				}
			}
		}
		
		echo 'exchangefail ok';
	}
	
	
	public function daoqitixingOp(){
		$condition = array();
		$condition['order_state'] = '0';
		$condition['is_fail'] = '0';
		$condition['daoqitixing'] = '0';
	
		$model = Model();
		$list = $model->table('order')->where($condition)->select();
	
		if(!empty($list)){
			foreach($list as $val){
				if((($val['add_time']+(3600*48))>time()) && (time()>$val['add_time']+(3600*40))){
					$model->table('order')->where(array('id'=>$val['id']))->update(array('daoqitixing'=>1));
					$user = $model->table('user')->where(array('user_id'=>$val['user_id']))->find();
					$auction = $model->table('auction')->where(array('id'=>$val['order_auction_id']))->find();
						
					$data = array();
					$data['touser'] = $user['openid'];
					$data['template_id'] = $this->getTemplateId(8);
					$data['url'] = BASE_SITE_URL.'/index.php?act=order&op=orderuserdetail&order_id='.$val['id'];
					$data['data']['first']['value'] = "尊敬的【{$user['nickname']}】";
					$data['data']['name']['value'] = '‘'.$auction['auction_description'].'’拍品，付款';
					$data['data']['expDate']['value'] = date("m月d日 H:i",($val['add_time']+(3600*48)));
					$data['data']['expDate']['color'] = '#1874CD';
					$data['data']['remark']['value'] = "付款逾期，订单将自动关闭";
						
					$data1 = json_encode($data);
					$this->send_template_message($data1);	
				}
			}
		}
	
	}
	
	/*
	 * 7天自动确认收货
	 * 小熊Shop team
	 */
	public function confirmshouhuoOp(){
		$condition = array();
		$condition['order_state'] = 2;
		
		$model = Model();
		$list = $model->table('order')->where($condition)->select();
		if(!empty($list)){
			foreach($list as $val){
				$seller = $model->table('user')->where(array('user_id'=>$val['seller_id']))->find();
				$order = $model->table('order')->where(array('id'=>$val['id']))->find();
				$auction = $model->table('auction')->where(array('id'=>$val['order_auction_id']))->find();
				if($val['is_delay']==1){
					if(($val['fahuo_time']+(3600*24*14))<time()){
						$params = array();
						$params['shouhuo_time'] = time();
						$params['order_state'] = 3;
							
						$model->table('order')->where(array('id'=>$val['id']))->update($params);
						
						
						$setting =  F('bearshopsetting');
						$is_charge = $setting['is_charge'];
							
						if($is_charge>0){//佣金比例
							$totalprice = $order['price']-($order['price']*$setting['is_charge']/100);
							$price = $seller['price']+$totalprice;
							$model->table('user')->where(array('user_id'=>$order['seller_id']))->update(array('price'=>$price));
						}else{
							$totalprice = $order['price'];
							$price = $seller['price']+$order['price'];
							$model->table('user')->where(array('user_id'=>$order['seller_id']))->update(array('price'=>$price));
						}
						//$model->table('user')->where(array('id'=>$val['seller_id']))->setInc('price',$val['price']);
						
						$params = array();
						$params['user_id'] = $order['seller_id'];
						$params['mingxi_type'] = '0';
						$params['mingxi_price'] = $totalprice;
						$params['add_time'] = time();
						$params['mingxi_reason'] = '拍品货款到账';
						$params['mingxi_pos'] = '0';
						$model->table('mingxi')->insert($params);
						
						if($auction['cashprice']>0){
							$model->table('user')->where(array('user_id'=>$val['seller_id']))->setInc('price',$auction['cashprice']);
							$model->table('user')->where(array('user_id'=>$val['user_id']))->setInc('price',$auction['cashprice']);							
						
							$params = array();
							$params['user_id'] = $val['seller_id'];
							$params['mingxi_type'] = '0';
							$params['mingxi_price'] = $val['cashprice'];
							$params['add_time'] = time();
							$params['mingxi_reason'] = '保证金退还';
							$params['mingxi_pos'] = '0';
							$model->table('mingxi')->insert($params);
								
							$params = array();
							$params['user_id'] = $val['user_id'];
							$params['mingxi_type'] = '0';
							$params['mingxi_price'] = $val['cashprice'];
							$params['add_time'] = time();
							$params['mingxi_reason'] = '保证金退还';
							$params['mingxi_pos'] = '0';
							$model->table('mingxi')->insert($params);
						}
						
						$stime = strtotime(date("Y-m-d"));
						$etime = $stime+3600*24;
						
						
						$s = array();
						$s['shouhuo_time'] = array('between',"{$stime},{$etime}");
						$s['order_state'] = 3;
						$s['seller_id'] = $val['seller_id'];
						$s['user_id'] = $val['user_id'];
						
						$r = $model->table('order')->where($s)->count();
						if($r<2){
							$upoints = ceil($val['price']/10);
							$user_condition = array();
							$user_condition['user_id'] = $val['user_id'];
							$model->table('user')->where($user_condition)->setInc('upoints',$upoints);
						
							$spoints = '';
							if($val['price']<250){
								$spoints = 1;
							}elseif(($val['price']<500)&&($val['price']>=250)){
								$spoints = 2;
							}elseif(($val['price']<2000)&&($val['price']>=500)){
								$spoints = 3;
							}elseif(($val['price']<16000)&&($val['price']>=2000)){
								$spoints = 4;
							}elseif($val['price']>=16000){
								$spoints = 5;
							}
							$seller_condition = array();
							$seller_condition['user_id'] = $val['seller_id'];
							$model->table('user')->where($seller_condition)->setInc('spoints',$spoints);
						}
						
						if($val['currentexchange']=='1'){
							$data = array();
							$data1 = array();
							$data['touser'] = $seller['openid'];
							$data['template_id'] = $this->getTemplateId(13);
							$data['url'] = BASE_SITE_URL.'/index.php?act=order&op=ordersellerdetail&auction_id='.$auction['id'];
							$data['data']['first']['value'] = "尊敬的【{$seller['nickname']}】,您的拍品买家已确认收货";
							$data['data']['keyword1']['value'] = "{$val['order_sn']}";
							$data['data']['keyword2']['value'] = "{$auction['auction_description']}";
							$data['data']['keyword3']['value'] = date("Y-m-d H:i",$val['add_time']);
							$data['data']['keyword4']['value'] = date("Y-m-d H:i",$val['fahuo_time']);
							$data['data']['keyword5']['value'] = date("Y-m-d H:i");
						
							$data1 = json_encode($data);
							$this->send_template_message($data1);
						}else{
							$data = array();
							$data1 = array();
							$data['touser'] = $seller['openid'];
							$data['template_id'] = $this->getTemplateId(13);
							$data['url'] = BASE_SITE_URL.'/index.php?act=order&op=ordersellerdetail&auction_id='.$auction['id'];
							$data['data']['first']['value'] = "尊敬的【{$seller['nickname']}】,您的拍品买家已确认收货";
							$data['data']['keyword1']['value'] = "{$val['order_sn']}";
							$data['data']['keyword2']['value'] = "{$auction['auction_description']}";
							$data['data']['keyword3']['value'] = date("Y-m-d H:i",$val['add_time']);
							$data['data']['keyword4']['value'] = date("Y-m-d H:i",$val['fahuo_time']);
							$data['data']['keyword5']['value'] = date("Y-m-d H:i");
							$data['data']['remark']['value'] = "提醒：交易完成，拍卖款项：{$totalprice}元，已经打入您的余额";
							$data1 = json_encode($data);
							$res = $this->send_template_message($data1);
						}
					}					
				}else{
					if(($val['fahuo_time']+(3600*24*7))<time()){
						$params = array();
						$params['shouhuo_time'] = time();
						$params['order_state'] = 3;
							
						$model->table('order')->where(array('id'=>$val['id']))->update($params);
						
						if($val['currentexchange']!='1'){
							$setting =  F('bearshopsetting');
							$is_charge = $setting['is_charge'];
							
							if($is_charge>0){//佣金比例
								$totalprice = $order['price']-($order['price']*$setting['is_charge']/100);
								$price = $seller['price']+$totalprice;
								$model->table('user')->where(array('user_id'=>$order['seller_id']))->update(array('price'=>$price));
							}else{
								$totalprice = $order['price'];
								$price = $seller['price']+$order['price'];
								$model->table('user')->where(array('user_id'=>$order['seller_id']))->update(array('price'=>$price));
							}
							
							//$model->table('user')->where(array('user_id'=>$val['seller_id']))->setInc('price',$val['price']);
							
							$params = array();
							$params['user_id'] = $val['seller_id'];
							$params['mingxi_type'] = '0';
							$params['mingxi_price'] = $totalprice;
							$params['add_time'] = time();
							$params['mingxi_reason'] = '拍品货款到账';
							$params['mingxi_pos'] = '0';
							$model->table('mingxi')->insert($params);
						}

						

						if($auction['cashprice']>0){
							$model->table('user')->where(array('user_id'=>$val['seller_id']))->setInc('price',$auction['cashprice']);
							$model->table('user')->where(array('user_id'=>$val['user_id']))->setInc('price',$auction['cashprice']);
						
							$params = array();
							$params['user_id'] = $val['seller_id'];
							$params['mingxi_type'] = '0';
							$params['mingxi_price'] = $val['cashprice'];
							$params['add_time'] = time();
							$params['mingxi_reason'] = '保证金退还';
							$params['mingxi_pos'] = '0';
							$model->table('mingxi')->insert($params);
							
							$params = array();
							$params['user_id'] = $val['user_id'];
							$params['mingxi_type'] = '0';
							$params['mingxi_price'] = $val['cashprice'];
							$params['add_time'] = time();
							$params['mingxi_reason'] = '保证金退还';
							$params['mingxi_pos'] = '0';
							$model->table('mingxi')->insert($params);
						}
							
						
						$stime = strtotime(date("Y-m-d"));
						$etime = $stime+3600*24;
												
						$s = array();
						$s['shouhuo_time'] = array('between',"{$stime},{$etime}");
						$s['order_state'] = 3;
						$s['seller_id'] = $val['seller_id'];
						$s['user_id'] = $val['user_id'];
						
						$r = $model->table('order')->where($s)->count();
						if($r<2){
							$upoints = ceil($val['price']/10);
							$user_condition = array();
							$user_condition['user_id'] = $val['user_id'];
							$model->table('user')->where($user_condition)->setInc('upoints',$upoints);
						
							$spoints = '';
							if($val['price']<250){
								$spoints = 1;
							}elseif(($val['price']<500)&&($val['price']>=250)){
								$spoints = 2;
							}elseif(($val['price']<2000)&&($val['price']>=500)){
								$spoints = 3;
							}elseif(($val['price']<16000)&&($val['price']>=2000)){
								$spoints = 4;
							}elseif($val['price']>=16000){
								$spoints = 5;
							}
							
							$seller_condition = array();
							$seller_condition['user_id'] = $val['seller_id'];
							$model->table('user')->where($seller_condition)->setInc('spoints',$spoints);
						}
						
						if($val['currentexchange']=='1'){
							$data = array();
							$data1 = array();
							$data['touser'] = $seller['openid'];
							$data['template_id'] = $this->getTemplateId(13);
							$data['url'] = BASE_SITE_URL.'/index.php?act=order&op=ordersellerdetail&auction_id='.$auction['id'];
							$data['data']['first']['value'] = "尊敬的【{$seller['nickname']}】,您的拍品买家已确认收货";
							$data['data']['keyword1']['value'] = "{$val['order_sn']}";
							$data['data']['keyword2']['value'] = "{$auction['auction_description']}";
							$data['data']['keyword3']['value'] = date("Y-m-d H:i",$val['add_time']);
							$data['data']['keyword4']['value'] = date("Y-m-d H:i",$val['fahuo_time']);
							$data['data']['keyword5']['value'] = date("Y-m-d H:i");
								
							$data1 = json_encode($data);
							$this->send_template_message($data1);
						}else{
							$data = array();
							$data1 = array();
							$data['touser'] = $seller['openid'];
							$data['template_id'] = $this->getTemplateId(13);
							$data['url'] = BASE_SITE_URL.'/index.php?act=order&op=ordersellerdetail&auction_id='.$auction['id'];
							$data['data']['first']['value'] = "尊敬的【{$seller['nickname']}】,您的拍品买家已确认收货";
							$data['data']['keyword1']['value'] = "{$val['order_sn']}";
							$data['data']['keyword2']['value'] = "{$auction['auction_description']}";
							$data['data']['keyword3']['value'] = date("Y-m-d H:i",$val['add_time']);
							$data['data']['keyword4']['value'] = date("Y-m-d H:i",$val['fahuo_time']);
							$data['data']['keyword5']['value'] = date("Y-m-d H:i");
							$data['data']['remark']['value'] = "提醒：交易完成，拍卖款项：{$totalprice}元，已经打入您的余额";
							$data1 = json_encode($data);
							$res = $this->send_template_message($data1);
						}
					}
					

				}			
			}	
		}		
		
		echo 'confirmshouhuo ok';
	}

	
	
	public function meifahuoOp(){
		
		$condition = array();
		$condition['order.order_state'] = '1';
		$condition['order.is_fail'] = '0';
		
		$model = Model();
		$field = 'order.price as price,order.payment_time as payment_time,order.order_sn as order_sn,order.order_state as order_state,order.add_time as add_time,auction.id as auction_id,auction.cashprice as cashprice,';
		$field.= 'order.user_id as user_id,order.seller_id as seller_id';
		$on = 'order.order_auction_id = auction.id';
		$list = $model->field($field)->table('order,auction')->join('left')->on($on)->where($condition)->select();
		
				
		if(!empty($list)){
			foreach($list as $val){
				if(($val['payment_time']+(3600*24*3))<time()){			
					$order_array = array();
					$order_array['finish_time'] = time();
					$order_array['is_fail'] = 1;
					$model->table('order')->where(array('order_auction_id'=>$val['auction_id']))->update($order_array);
					
					$price = $val['price'];
					if($val['cashprice']>0){
						$price = $price+$val['cashprice']*2;						
					}
					
					$model->table('user')->where(array('user_id'=>$val['user_id']))->setInc('price',$price);
			
					$params = array();
					$params['user_id'] = $val['user_id'];
					$params['mingxi_type'] = '0';
					$params['mingxi_price'] = $price;
					$params['add_time'] = time();
					$params['mingxi_reason'] = '卖家未发货，保证金退还和赔偿';
					$params['mingxi_pos'] = '0';
					$model->table('mingxi')->insert($params);				
				}
			}
		}
		
		echo 'meifahuo ok';		
	}
	
	
	/*
	 * 买家退货，卖家同意，买家未发货
	 */
	public function usermeifahuoOp(){
		
		$model = Model();
		$condition = array();
		$condition['is_tuihuo'] = 1;
		$condition['is_fail'] = 0;
		$condition['is_accept'] = 1;
		$condition['order_state'] = 2;
		
		$list = $model->table('order')->where($condition)->select();
		if(!empty($list)){
			foreach($list as $key=>$val){
				$seller = $model->table('user')->where(array('user_id'=>$val['seller_id']))->find();
				$auction = $model->table('auction')->where(array('id'=>$val['order_auction_id']))->find();
				$order = $model->table('order')->where(array('id'=>$val['id']))->find();
				if($val['accept_time']<(time()-2*24*3600)){
					$params = array();
					$params['shouhuo_time'] = time();
					$params['order_state'] = 3;
						
					$model->table('order')->where(array('id'=>$val['id']))->update($params);
					
					if($val['currentexchange']!='1'){
						
						$setting =  F('bearshopsetting');
						$is_charge = $setting['is_charge'];
							
						if($is_charge>0){//佣金比例
							$totalprice = $order['price']-($order['price']*$setting['is_charge']/100);
							$price = $seller['price']+$totalprice;
							$model->table('user')->where(array('user_id'=>$order['seller_id']))->update(array('price'=>$price));
						}else{
							$totalprice = $order['price'];
							$price = $seller['price']+$order['price'];
							$model->table('user')->where(array('user_id'=>$order['seller_id']))->update(array('price'=>$price));
						}
						
						//$model->table('user')->where(array('user_id'=>$val['seller_id']))->setInc('price',$val['price']);
							
						$params = array();
						$params['user_id'] = $val['seller_id'];
						$params['mingxi_type'] = '0';
						$params['mingxi_price'] = $totalprice;
						$params['add_time'] = time();
						$params['mingxi_reason'] = '拍品货款到账';
						$params['mingxi_pos'] = '0';
						$model->table('mingxi')->insert($params);
					}
					
					
					if($auction['cashprice']>0){
						$model->table('user')->where(array('user_id'=>$val['seller_id']))->setInc('price',$auction['cashprice']);
						$model->table('user')->where(array('user_id'=>$val['user_id']))->setInc('price',$auction['cashprice']);
					
						$params = array();
						$params['user_id'] = $val['seller_id'];
						$params['mingxi_type'] = '0';
						$params['mingxi_price'] = $val['cashprice'];
						$params['add_time'] = time();
						$params['mingxi_reason'] = '保证金退还';
						$params['mingxi_pos'] = '0';
						$model->table('mingxi')->insert($params);
							
						$params = array();
						$params['user_id'] = $val['user_id'];
						$params['mingxi_type'] = '0';
						$params['mingxi_price'] = $val['cashprice'];
						$params['add_time'] = time();
						$params['mingxi_reason'] = '保证金退还';
						$params['mingxi_pos'] = '0';
						$model->table('mingxi')->insert($params);
					}

						
					
					$stime = strtotime(date("Y-m-d"));
					$etime = $stime+3600*24;
					
					$s = array();
					$s['shouhuo_time'] = array('between',"{$stime},{$etime}");
					$s['order_state'] = 3;
					$s['seller_id'] = $val['seller_id'];
					$s['user_id'] = $val['user_id'];
					
					$r = $model->table('order')->where($s)->count();
					if($r<2){
						$upoints = ceil($val['price']/10);
						$user_condition = array();
						$user_condition['user_id'] = $val['user_id'];
						$model->table('user')->where($user_condition)->setInc('upoints',$upoints);
					
						$spoints = '';
						if($val['price']<250){
							$spoints = 1;
						}elseif(($val['price']<500)&&($val['price']>=250)){
							$spoints = 2;
						}elseif(($val['price']<2000)&&($val['price']>=500)){
							$spoints = 3;
						}elseif(($val['price']<16000)&&($val['price']>=2000)){
							$spoints = 4;
						}elseif($val['price']>=16000){
							$spoints = 5;
						}
							
						$seller_condition = array();
						$seller_condition['user_id'] = $val['seller_id'];
						$model->table('user')->where($seller_condition)->setInc('spoints',$spoints);
					}
					

					$data = array();
					$data1 = array();
					$data['touser'] = $seller['openid'];
					$data['template_id'] = $this->getTemplateId(13);
					$data['url'] = BASE_SITE_URL.'/index.php?act=order&op=ordersellerdetail&auction_id='.$auction['id'];
					$data['data']['first']['value'] = "尊敬的【{$seller['nickname']}】,您的拍品买家已确认收货";
					$data['data']['keyword1']['value'] = "{$val['order_sn']}";
					$data['data']['keyword2']['value'] = "{$auction['auction_description']}";
					$data['data']['keyword3']['value'] = date("Y-m-d H:i",$val['add_time']);
					$data['data']['keyword4']['value'] = date("Y-m-d H:i",$val['fahuo_time']);
					$data['data']['keyword5']['value'] = date("Y-m-d H:i");
					$data['data']['remark']['value'] = "提醒：交易完成，拍卖款项：{$totalprice}元，已经打入您的余额";
					$data1 = json_encode($data);
					$res = $this->send_template_message($data1);
				}
			}
		}
	}
	
	
	/*
	 * 卖家未确认退货
	 */	
	public function meidealtuiOp(){
		
		$model = Model();
		$condition = array();
		$condition['is_tuihuo'] = 1;
		$condition['is_fail'] = 0;
		$condition['is_accept'] = 1;
		$list = $model->table('order')->where($condition)->select();
		
		
		if(!empty($list)){
			foreach($list as $key=>$val){
				if((!empty($val['tuihuo_expresscode']))&&($val['tuihuo_expresstime']<(time()-7*24*3600))){
					$auction = $model->table('auction')->where(array('id'=>$val['order_auction_id']))->find();
					$user = $model->table('user')->where(array('user_id'=>$val['user_id']))->find();
						
					$price = $val['price'];
					if($auction['cashprice']>0){
						$seller = $model->table('user')->where(array('user_id'=>$val['seller_id']))->find();
							
						$data = array();
						$data1 = array();
						$data['touser'] = $seller['openid'];
						$data['template_id'] = $this->getTemplateId(6);
						$data['url'] = BASE_SITE_URL.'/index.php?act=order&op=ordersellerdetail&auction_id='.$val['id'];
						$data['data']['first']['value'] = "尊敬的【{$seller['nickname']}】,您的保证金额退回至余额";
						$data['data']['reason']['value'] = '退款';
						$data['data']['refund']['value'] = $auction['cashprice'].'元';
						$data['data']['remark']['value'] = '如未收到，请联系客服';
							
						$data1 = json_encode($data);
						$this->send_template_message($data1);
						$model->table('user')->where(array('user_id'=>$val['seller_id']))->setInc('price',$auction['cashprice']);
							
						$params = array();
						$params['user_id'] = $val['seller_id'];
						$params['mingxi_type'] = '0';
						$params['mingxi_price'] = $auction['cashprice'];
						$params['add_time'] = time();
						$params['mingxi_reason'] = '保证金退还';
						$params['mingxi_pos'] = '0';
						$model->table('mingxi')->insert($params);
							
						$price = $price+$auction['cashprice'];
					}
						
					$user['price']=$user['price']+$price;
						
					$model->table('user')->where(array('user_id'=>$user['user_id']))->update(array('price'=>$user['price']));
					
					$params = array();
					$params['is_fail'] = 1;
					$params['is_deal'] = 1;
					$params['finish_time'] = time();
						
					$model->table('order')->where(array('id'=>$val['id']))->update($params);
						
					$data = array();
					$data1 = array();
					$data['touser'] = $user['openid'];
					$data['template_id'] = $this->getTemplateId(6);
					$data['url'] = BASE_SITE_URL.'/index.php?act=order&op=orderuserdetail&order_id='.$val['id'];
					$data['data']['first']['value'] = "尊敬的【{$user['nickname']}】,您的退款金额退回至余额";
					$data['data']['reason']['value'] = '退款';
					$data['data']['refund']['value'] = $price.'元';
					$data['data']['remark']['value'] = '如未收到退款，请联系客服';
						
					$data1 = json_encode($data);
					$this->send_template_message($data1);
						
					$params = array();
					$params['user_id'] = $user['user_id'];
					$params['mingxi_type'] = '0';
					$params['mingxi_price'] = $price;
					$params['add_time'] = time();
					$params['mingxi_reason'] = '拍品金额和保证金退还';
					$params['mingxi_pos'] = '0';
					$model->table('mingxi')->insert($params);
				}
			}
		}
		
	}
	
	/*
	 * 卖家未处理退货申请
	 */
	public function meidealtuikuanOp(){
		
		$model = Model();
		$condition = array();
		$condition['is_tuihuo'] = 1;
		$condition['is_fail'] = 0;
		
		$list = $model->table('order')->where($condition)->select();
		if(!empty($list)){
			foreach($list as $key=>$val){
				if($val['tuihuo_time']<(time()-48*3600)){
					if(empty($val['is_accept'])){
						$params = array();
						$params['is_accept'] = 1;
						$params['accept_time'] = time();
						
						$address = $model->table('address')->where(array('user_id'=>$val['seller_id'],'is_default'=>1))->find();
												
						$params['tuihuo_address'] = $address['id'];						
// 						$params['is_fail'] = 1;
// 						$params['is_deal'] = 1;
// 						$params['finish_time'] = time();
						
						$res = $model->table('order')->where(array('id'=>$val['id']))->update($params);
						if($res){
							$auction = $model->table('auction')->where(array('id'=>$val['order_auction_id']))->find();
							$user = $model->table('user')->where(array('user_id'=>$val['user_id']))->find();
							
							$data = array();
							$data1 = array();
							$data['touser'] = $user['openid'];
							$data['template_id'] = $this->getTemplateId(6);
							$data['url'] = BASE_SITE_URL.'/index.php?act=order&op=orderuserdetail&order_id='.$val['id'];
							$data['data']['first']['value'] = "尊敬的【{$user['nickname']}】,拍品“{$auction['auction_description']}”卖家已同意退货，请在".date("m月d日 H:i",($params['accept_time']+48*3600)).'前，填写拍品退货单号。若超期，该拍品将自动确认收货！';
							$data['data']['reason']['value'] = '申请退货';
							$data['data']['refund']['value'] = "¥{$val['price']}元";
							$data['data']['remark']['value'] = '您的货款将在卖家收到拍品后退还（退货运费由您自行承担）';
								
							$data1 = json_encode($data);
							$this->send_template_message($data1);
							echo json_encode(array('state'=>1));
							exit;
						}						
					}
				}
			}
		}
		
	}
	
	
	public function confirmcommentOp(){
		$condition = array();
		$condition['order_state'] = 3;
		$model = Model();
		$list = $model->table('order')->where($condition)->select();
		
		if(!empty($list)){
			foreach($list as $val){
				if(($val['shouhuo_time']+(3600*24*7))<time() && empty($val['order_comment'])){
					$params = array();
					$params['order_comment'] = '好评';
					$params['comment_time'] = time();
					$params['order_score'] = '5';
					
					$model->table('order')->where(array('id'=>$val['id']))->update($params);
				}
			}
		}
	}
	
	/*
	 * 付款通知地址
	 */
	public function notifyOp(){	
		include_once(BASE_ROOT_PATH."/shop/templates/default/home/WxPayPubHelper/WxPayPubHelper.php");

		
		$notify = new Notify_pub();
		$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
		$notify->saveData($postStr);
		if($notify->checkSign() == FALSE){
			echo 'FAIL';
			exit;
		}
		
		$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
		$postObj = (array)$postObj;

		
		$order_sn = $postObj['out_trade_no'];
		$model = Model();
		$condition = array();
		$condition['order_sn'] = $order_sn;
		$condition['order_state'] = '0';
		
		$order = $model->table('order')->where($condition)->find();
		if(empty($order)){
			exit;
		}
		
		$update = array();
		$update['order_state'] = 1;
		$update['payment_time'] = time();
		$update['currentexchange'] = 0;
		$res = $model->table('order')->where(array('id'=>$order['id']))->update($update);
		if($res){
			echo 'success';
		}
		
	
		$auction = $model->table('auction')->where(array('id'=>$order['order_auction_id']))->find();
		$message = '买家已经付款! <br>尊敬的卖家，您的拍品买家已经付款';
		$this->addsellerlog($order['order_auction_id'],$message,$auction['seller_id']);
		
		$address = $model->table('address')->where(array('id'=>$order['order_address']))->find();
		$prov = $model->table('region')->where(array('region_code'=>$address['pid']))->find();
		$address['prov_name'] = $prov['region_name'];
		
		$city = $model->table('region')->where(array('region_code'=>$address['cid']))->find();
		$address['city_name'] = $city['region_name'];
		
		if(!empty($address['did'])){
			$dis = $model->table('region')->where(array('region_code'=>$address['did']))->find();
			$address['dis_name'] = $dis['region_name'];
		}else{
			$address['dis_name'] = '';
		}
		
		$user = $model->table('user')->where(array('user_id'=>$order['user_id']))->find();		
		$params = array();
		$params['user_id'] = $order['user_id'];
		$params['mingxi_type'] = '1';
		$params['mingxi_price'] = $order['price'];
		$params['add_time'] = time();
		$params['mingxi_reason'] = '拍品支付';
		$params['mingxi_pos'] = '0';
		$model->table('mingxi')->insert($params);
		
		$seller = $model->table('user')->where(array('user_id'=>$auction['seller_id']))->find();
		$data = array();
		$data1 = array();
		$data['touser'] = $seller['openid'];
		$data['template_id'] = $this->getTemplateId(10);
		$data['url'] = BASE_SITE_URL.'/index.php?act=order&op=ordersellerdetail&auction_id='.$order['order_auction_id'];
		$data['data']['first']['value'] = "尊敬的【{$seller['nickname']}】,买家【{$user['nickname']}】已经付款成功!";
		$data['data']['orderProductPrice']['value'] = "{$order['price']}";
		$data['data']['orderProductName']['value'] = "{$auction['auction_description']}";
		$data['data']['orderAddress']['value'] = "{$address['prov_name']}{$address['city_name']}{$address['dis_name']}{$address['address']}";
		$data['data']['orderName']['value'] = "{$order['order_sn']}";
		$data1 = json_encode($data);
		$this->send_template_message($data1);
		
		if($auction['cashprice']>0){
			//买家保证金退还
			$user_cash_log = $model->table('cash_log')->where(array('user_id'=>$order['user_id'],'auction_id'=>$auction['id']))->find();
			if($user_cash_log['paymentname']=='weixin'){
				require_once BASE_ROOT_PATH."/shop/templates/default/home/WxPayPubHelper/WxPayPubHelper.php";
				$out_trade_no = $user_cash_log['pre_order_sn'];
				$refund_fee = $user_cash_log["cprice"]*100;
				//商户退款单号，商户自定义，此处仅作举例
				$out_refund_no = "refund_ucash_sn".time();
				//总金额需与订单号out_trade_no对应，demo中的所有订单的总金额为1分
				$total_fee = $user_cash_log["cprice"]*100;
				//使用退款接口
				$refund = new Refund_pub();
				$refund->setParameter("out_trade_no","$out_trade_no");//商户订单号
				$refund->setParameter("out_refund_no","$out_refund_no");//商户退款单号
				$refund->setParameter("total_fee","$total_fee");//总金额
				$refund->setParameter("refund_fee","$refund_fee");//退款金额
				$refund->setParameter("op_user_id",WxPayConf_pub::MCHID);//操作员
				//调用结果
				$refundResult = $refund->getResult();
					
				$data = array();
				$data1 = array();
				$data['touser'] = $user['openid'];
				$data['template_id'] = $this->getTemplateId(6);
				$data['url'] = BASE_SITE_URL.'/index.php?act=order&op=orderuserdetail&order_id='.$order['id'];
				$data['data']['first']['value'] = "尊敬的【{$user['nickname']}】,您参与的‘{$auction['auction_description']}’拍卖付款成功，保证金退还至您微信钱包";
				$data['data']['reason']['value'] = '付款成功';
				$data['data']['refund']['value'] = $auction['cashprice'].'元';
				$data['data']['remark']['value'] = '如未收到退款，请联系客服';
					
				//明细记录
				$params = array();
				$params['user_id'] = $order['user_id'];
				$params['mingxi_type'] = '0';
				$params['mingxi_price'] = $auction['cashprice'];
				$params['add_time'] = time();
				$params['mingxi_reason'] = '保证金退还';
				$params['mingxi_pos'] = '1';
				$model->table('mingxi')->insert($params);
					
			}else{
				$model->table('user')->where(array('user_id'=>$order['user_id']))->setInc('price',$auction['cashprice']);
					
				$data = array();
				$data1 = array();
				$data['touser'] = $user['openid'];
				$data['template_id'] = $this->getTemplateId(6);
				$data['url'] = BASE_SITE_URL.'/index.php?act=order&op=orderuserdetail&order_id='.$order['id'];
				$data['data']['first']['value'] = "尊敬的【{$user['nickname']}】,您参与的‘{$auction['auction_description']}’拍卖付款成功，保证金退还至您的余额";
				$data['data']['reason']['value'] = '付款成功';
				$data['data']['refund']['value'] = $auction['cashprice'].'元';
				$data['data']['remark']['value'] = '如未收到退款，请联系客服';
					
				//明细记录
				$params = array();
				$params['user_id'] = $order['user_id'];
				$params['mingxi_type'] = '0';
				$params['mingxi_price'] = $auction['cashprice'];
				$params['add_time'] = time();
				$params['mingxi_reason'] = '保证金退还';
				$params['mingxi_pos'] = '0';
				$model->table('mingxi')->insert($params);
			}
				
			$data1 = json_encode($data);
			$this->send_template_message($data1);
		}
		
		exit;
	}
	
	public function cashnotifyOp(){
		include_once(BASE_ROOT_PATH."/shop/templates/default/home/WxPayPubHelper/WxPayPubHelper.php");

		
		$notify = new Notify_pub();
		$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
		$notify->saveData($postStr);
		if($notify->checkSign() == FALSE){
			echo 'FAIL';
			exit;
		}
		
		$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
		$postObj = (array)$postObj;
		//echo 'success';
		$model = Model();
		$pre_order_sn = $postObj['out_trade_no'];
		$condition = array();
		$condition['pre_order_sn'] = $pre_order_sn;
		$condition['is_cash'] = '0';
		
		$cash_log = $model->table('cash_log')->where($condition)->find();
		if(empty($cash_log)){
			exit;
		}

		
		$res = $model->table('cash_log')->where(array('id'=>$cash_log['id']))->update(array('is_cash'=>1));
		$auction = $model->table('auction')->where(array('id'=>$cash_log['auction_id']))->find();
		

		
		if($auction['seller_id']==$cash_log['user_id']){
			$update = array();
			$update['is_fabu'] = 1;
			$update['add_time'] = time();
			$update['is_state'] = '0';
			$model->table('auction')->where(array('id'=>$cash_log['auction_id']))->update($update);
			
			$params = array();
			$params['user_id'] = $cash_log['user_id'];
			$params['mingxi_type'] = '1';
			$params['mingxi_price'] = $auction['cashprice'];
			$params['add_time'] = time();
			$params['mingxi_reason'] = '保证金支付';
			$params['mingxi_pos'] = '1';
			$model->table('mingxi')->insert($params);
			
			$this->tuiinfo($auction['id']);
		}else{
			
			
			
			$user = $model->table('user')->where(array('user_id'=>$cash_log['user_id']))->find();
			$auction_price = $postObj['attach'];
			
			$auction_log = $model->table('auction_log')->where(array('auction_id'=>$auction['id']))->order('id desc')->find();
			if($auction_log['auction_price']>=$auction_price){
				echo 'success';
				exit;
			}
			
			$data = array();
			$data['auction_id'] = $auction['id'];
			$data['auction_price'] = $auction_price;
			$data['add_time'] = time();
			$data['user_id'] = $user['user_id'];
			$data['nickname'] = $user['nickname'];
			$data['avatar'] = $user['avatar'];
			
			$res = $model->table('auction_log')->insert($data,true);
			
			$params = array();
			$params['user_id'] = $cash_log['user_id'];
			$params['mingxi_type'] = '1';
			$params['mingxi_price'] = $auction['cashprice'];
			$params['add_time'] = time();
			$params['mingxi_reason'] = '保证金支付';
			$params['mingxi_pos'] = '1';
			$model->table('mingxi')->insert($params);
			
			if($res){
				
				
				
				$update1 = array();
				$update1['leadingprice'] = $auction_price;
				$update1['auction_log_id'] = $res;
				if(($auction['yikoujia']>0)&&($auction_price>=$auction['yikoujia'])){
					$update1['yikoujia_end'] = 1;
				}
				
				if(($auction['end_time']-300)<time()){
					$update1['end_time'] = $auction['end_time']+300;
				}
				
				$model->table('auction')->where(array('id'=>$auction['id']))->update($update1);
				if($update1['yikoujia_end'] ==1){
					echo 'success';
					exit;
				}
				
				$seller = $model->table('user')->where(array('user_id'=>$auction['seller_id']))->find();
				
				$data = array();
				$data['touser'] = $seller['openid'];
				$data['template_id'] = $this->getTemplateId(11);
				$data['url'] = BASE_SITE_URL.'/index.php?act=index&auction_id='.$auction['id'];
				$data['data']['first']['value'] = "您的拍品【{$seller['nickname']}】出价{$auction_price}元，目前领先";
				$data['data']['number']['value'] = "{$auction['auction_sn']}";
				$data['data']['name']['value'] = "{$auction['auction_description']}";
				$data['data']['remark']['value'] = '提醒：'.date("m月d日 H:i",$auction['end_time']).'拍卖结束';
				$data1 = json_encode($data);
				$res = $this->send_template_message($data1);
				
				$seller_log = array();
				$seller_log['auction_id'] = $auction['id'];
				$seller_log['uid'] = $auction['seller_id'];
				$seller_log['message'] = $data['data']['first']['value'];
				$seller_log['add_time'] = time();
				$model->table('seller_log')->insert($seller_log);
				
				$condition = array();
				$condition['auction_log.auction_id'] = $auction['id'];
				$field = 'auction_log.id as id,user.openid as openid';
				$on = 'auction_log.user_id = user.user_id';
				$userlist = $model->field($field)->table('auction_log,user')->join('left')->on($on)->where($condition)->select();
				
				if(!empty($userlist)){
					$arr = array();
					foreach($userlist as $vvv){				
						if($user['openid']==$vvv['openid']){
							continue;
						}
						
						if(in_array($vvv['openid'],$arr)){
							continue;
						}
				
						$data = array();
						$data['touser'] = $vvv['openid'];
						$data['template_id'] = $this->getTemplateId(11);
						$data['url'] = BASE_SITE_URL.'/index.php?act=index&auction_id='.$auction['id'];
						$data['data']['first']['value'] = "【{$user['nickname']}】出价{$auction_price}元，目前领先";
						$data['data']['number']['value'] = "{$auction['auction_sn']}";
						$data['data']['name']['value'] = "{$auction['auction_description']}";
						$data['data']['remark']['value'] = '提醒：'.date("m月d日 H:i",$auction['end_time']).'拍卖结束';;
						$data1 = json_encode($data);
						$this->send_template_message($data1);
						$arr[] = $vvv['openid'];
					}
				}

			}
		}
		
		echo 'success';
		exit;
	}
	
	public function companynotifyOp(){
		include_once(BASE_ROOT_PATH."/shop/templates/default/home/WxPayPubHelper/WxPayPubHelper.php");

		
		$notify = new Notify_pub();
		$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
		$notify->saveData($postStr);
		if($notify->checkSign() == FALSE){
			echo 'FAIL';
			exit;
		}
		
		$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
		$postObj = (array)$postObj;
		
		$model = Model();
		$company_order_sn = $postObj['out_trade_no'];
		
		$condition = array();
		$condition['company_order_sn'] = $company_order_sn;
		$company = $model->table('company')->where($condition)->find();
		$user = $model->table('user')->where(array('user_id'=>$company['user_id']))->find();
		if($company['company_state']==3){			
			$params = array();
			$params['company_valid'] = strtotime(date('Y') + 1 . '-' . date('m-d H:i:s',$user['company_valid']));		
			$model->table('user')->where(array('user_id'=>$company['user_id']))->update($params);
			echo 'success';
			exit;
			
		}else{
			$company_params = array();
			$company_params['company_state'] = 3;
			$company_params['company_add_time'] = time();
			
			$res = $model->table('company')->where($condition)->update($company_params);
			if($res){
			
				$params = array();
				$params['is_company'] = 1;
				$params['is_company_type'] = $company['company_type'];
				$params['company_valid'] = strtotime(date('Y') + 1 . '-' . date('m-d H:i:s'));
					
				$model->table('user')->where(array('user_id'=>$company['user_id']))->update($params);					
				
				$wx_name_arr = F('wxaccount');
				$wx_name = $wx_name_arr['wx_name'];
				
				$content = "实名认证申请通知：\n{$user['nickname']},恭喜！您({$wx_name})店铺实名认证已通过。\n";
				$content.="<a href='".BASE_SITE_URL."/index.php?act=renzheng&op=renzheng'>查看更多认证详情</a>";
				
				$txt = '{
					"touser":"'.$user['openid'].'",
					"msgtype":"text",
					"text":{
						"content":"'.$content.'"
					}
				}';
				
				$access_token = $this->getAccessTokenIn();
				
				$url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={$access_token}";
				$d = $this->https_request($url,$txt);
				$d = json_decode($d,true);
				if($d['errmsg']!='ok'){
					$access_token = $this->getAccessToken();
					$url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={$access_token}";
					$this->https_request($url,$txt);
				}
				echo 'success';
			}
			exit;
		}
	}
	
	public function rechargenotifyOp(){

		include_once(BASE_ROOT_PATH."/shop/templates/default/home/WxPayPubHelper/WxPayPubHelper.php");

		
		$notify = new Notify_pub();
		$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
		$notify->saveData($postStr);
		if($notify->checkSign() == FALSE){
			echo 'FAIL';
			exit;
		}
		
		$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
		$postObj = (array)$postObj;
		
		$model = Model();
		$recharge_order_sn = $postObj['out_trade_no'];
		$condition = array();
		$condition['recharge_order_sn'] = $recharge_order_sn;
		$condition['is_recharge'] = '0';
		
		$recharge = $model->table('recharge')->where($condition)->find();
		if(empty($recharge)){
			exit;
		}
		
		$user = $model->table('user')->where(array('user_id'=>$recharge['user_id']))->find();
		$price = $user['price']+$recharge['price'];

		$params = array();
		$params['price'] = $price;
		$res1 = $model->table('user')->where(array('user_id'=>$user['user_id']))->update($params);
		if($res1){
			$params = array();
			$params['user_id'] = $user['user_id'];
			$params['mingxi_type'] = '0';
			$params['mingxi_price'] = $recharge['price'];
			$params['add_time'] = time();
			$params['mingxi_reason'] = '充值';
			$params['mingxi_pos'] = '1';
			$model->table('mingxi')->insert($params);
		}	
		$res2 = $model->table('recharge')->where(array('id'=>$recharge['id']))->update(array('is_recharge'=>1));
		
		if($res1&&$res2){
			echo 'success';
		}
		
		exit;		
	}	


	public function shuiyinOp(){
		/*给图片加文字水印的方法*/
		$dst_path = $_GET['imageurl'];
		$dst = imagecreatefromstring(file_get_contents($dst_path));
		/*imagecreatefromstring()--从字符串中的图像流新建一个图像，返回一个图像标示符，其表达了从给定字符串得来的图像
		 图像格式将自动监测，只要php支持jpeg,png,gif,wbmp,gd2.*/
		
		$font = './FZDHTK.TTF';
		$wx_name_arr = F('wxaccount');
		$wx_name = $wx_name_arr['wx_name'];
		$black = imagecolorallocate($dst, 255,255,255);
		imagefttext($dst, 30, 0, 500,800, $black, $font, $wx_name);
		/*imagefttext($img,$size,$angle,$x,$y,$color,$fontfile,$text)
		 $img由图像创建函数返回的图像资源
		size要使用的水印的字体大小
		angle（角度）文字的倾斜角度，如果是0度代表文字从左往右，如果是90度代表从上往下
		x,y水印文字的第一个文字的起始位置
		color是水印文字的颜色
		fontfile，你希望使用truetype字体的路径*/
		$dst_type = 2;
		list($dst_w,$dst_h,$dst_type) = getimagesize($dst_path);
		/*list(mixed $varname[,mixed $......])--把数组中的值赋给一些变量
		 像array()一样，这不是真正的函数，而是语言结构，List()用一步操作给一组变量进行赋值*/
		/*getimagesize()能获取到什么信息？
		 getimagesize函数会返回图像的所有信息，包括大小，类型等等*/
		switch($dst_type){
			case 1://GIF
				header("content-type:image/gif");
				imagegif($dst);
				break;
			case 2://JPG
				header("content-type:image/jpeg");
				imagejpeg($dst);
				break;
			case 3://PNG
				header("content-type:image/png");
				imagepng($dst);
				break;
			default:
				break;
				/*imagepng--以PNG格式将图像输出到浏览器或文件
				 imagepng()将GD图像流(image)以png格式输出到标注输出（通常为浏览器），或者如果用filename给出了文件名则将其输出到文件*/
		}
		imagedestroy($dst);
	}
	
	public function renzhengOp(){
		include "./phpqrcode.php";
		$url = BASE_SITE_URL.'/index.php?act=my&op=renzheng';
		QRcode::png($url);
		
	}
	
	
	/*
	 * 清空摇一摇数据
	 * 小熊Shop
	 */
	public function clearyaoOp(){
		
		$date = date("Y-m-d")." 00:00:00";
		$date = strtotime($date);
		$model = Model();
		$yao = $model->table('yao')->order('id desc')->find();
		
		if($yao['add_time']<$date){
			$model->table('yao')->clear();
		}
		
		echo 'clear ok';
				
	}
	
	
}